package com.rhb.erp.hr.department.web.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;
import java.text.*;
import org.apache.struts.action.*;

import com.rhb.erp.hr.department.bean.*;
import com.rhb.erp.hr.department.web.struts.form.DepartmentForm;
import com.rhb.erp.web.struts.action.ErpBaseDispatchAction;


public class DepartmentControlAction extends ErpBaseDispatchAction {
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		//System.out.println("unspecified for defalut parameter!");
		return edit(mapping,form,request,response);
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		DepartmentForm eForm = (DepartmentForm)form;

		int allcount = this.getDepartmentBusiness().getAllCount_findByQuery(eForm.getDepartmentQuery());
		List list = null;
		if ( allcount != 0) {
			list = this.getDepartmentBusiness().findByQuery(eForm.getDepartmentQuery(),eForm.getStart(),eForm.getCount());
		}else{
			list = (List)new ArrayList();
		}

		eForm.setAllCount(allcount);
		eForm.setList(list);
		return mapping.findForward("list");
	}


	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		DepartmentForm oform = (DepartmentForm)form;
		oform.setDepartment(new Department());
		oform.setParentPk(null);
		oform.setParentName(null);
		//oform.reset(mapping, request);
		oform.setDepartmentXMLDoc(this.getDepartmentBusiness().getDepartmentXMLDoc());
		return mapping.findForward("add");
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		DepartmentForm oform = (DepartmentForm)form;
		
		oform.setDepartmentXMLDoc(this.getDepartmentBusiness().getDepartmentXMLDoc());
		
		if(oform.getDepartment()==null){
			oform.setDepartment(new Department());
		}
		
		if(oform.getDepartment().getDepartmentPk()!=null && !"".equals(oform.getDepartment().getDepartmentPk())){
			oform.setDepartment(this.getDepartmentBusiness().findByPk(oform.getDepartment().getDepartmentPk()));
			Department parent = oform.getDepartment().getParent();
			if(parent!=null){
				oform.setParentPk(parent.getDepartmentPk());
				oform.setParentName(parent.getNameMap());
			}else{
				oform.setParentPk(null);
				oform.setParentName(null);
			}
		}
		
	
		return mapping.findForward("edit");
	}
	
	public ActionForward view(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		DepartmentForm oform = (DepartmentForm)form;
		oform.setDepartment(this.getDepartmentBusiness().findByPk(oform.getDepartment().getDepartmentPk()));
		Department parent = oform.getDepartment().getParent();
		if(parent!=null){
			oform.setParentPk(parent.getDepartmentPk());
			oform.setParentName(parent.getNameMap());
		}
		return mapping.findForward("view");
	}
	
	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		return mapping.findForward("del");
	}
	
	public ActionForward json(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		DepartmentForm eForm = (DepartmentForm)form;

		int allcount = this.getDepartmentBusiness().getAllCount_findByQuery(eForm.getDepartmentQuery());
		List list = null;
		if ( allcount != 0) {
			list = this.getDepartmentBusiness().findByQuery(eForm.getDepartmentQuery(),eForm.getStart(),eForm.getCount());
		}else{
			list = (List)new ArrayList();
		}

		eForm.setAllCount(allcount);
		eForm.setList(list);
		return mapping.findForward("json");
	}
}
